﻿using System.IO;
using System.Text;
using Best.SmartCms.CodeGeneration.Model;
using Best.SmartCms.CodeGeneration.Template;

namespace Best.SmartCms.CodeGeneration.BuiderFactory
{
    public class ModifyFileBuilder : CodeGenerationFactory
    {
        public override void BuilderTemplate(CodeGenerationDto dto)
        {
            if (dto.IsBulderDomain)
            {
                var dataSetPath = Path.Combine(BasePath, "Best.SmartCms.Domain/MySqlDbContext.cs");
                string content = File.ReadAllText(dataSetPath);
                string newConten = content.Replace("// CodeGeneration_Bookmark_Set代码生成书签勿删除无修改并独立一行", $"public DbSet<{dto.DomainName}> {dto.DomainName}Set {{ get; set; }}{System.Environment.NewLine}        // CodeGeneration_Bookmark_Set代码生成书签勿删除无修改并独立一行");
                File.WriteAllText(dataSetPath, newConten, Encoding.UTF8);
            }

            if (dto.IsBulderJs)
            {
                var modulePath = Path.Combine(BasePath,"Best.SmartCms.Admin/ClientApp/src/app/routes/routes.module.ts");
                string content = File.ReadAllText(modulePath);
                content = content.Replace("// CodeGeneration_Bookmark_Import代码生成书签勿删除", $"//{dto.DomainName}{ System.Environment.NewLine}import {{ {dto.DomainName}Component }} from './{dto.DomainName.ToLower()}/{dto.DomainName.ToLower()}.component';{ System.Environment.NewLine}import {{ Create{dto.DomainName}Component }} from './{dto.DomainName.ToLower()}/create-{dto.DomainName.ToLower()}.component';{ System.Environment.NewLine}import {{ Edit{dto.DomainName}Component}} from './{dto.DomainName.ToLower()}/edit-{dto.DomainName.ToLower()}.component';{ System.Environment.NewLine}// CodeGeneration_Bookmark_Import代码生成书签勿删除");
                content = content.Replace("// CodeGeneration_Bookmark_Components代码生成书签勿删除", $"//{dto.DomainName}{ System.Environment.NewLine}{dto.DomainName}Component,{ System.Environment.NewLine}Create{dto.DomainName}Component,{ System.Environment.NewLine}Edit{dto.DomainName}Component,{ System.Environment.NewLine}// CodeGeneration_Bookmark_Components代码生成书签勿删除");
                File.WriteAllText(modulePath, content, Encoding.UTF8);


                var routingPath = Path.Combine(BasePath,"Best.SmartCms.Admin/ClientApp/src/app/routes/routes-routing.module.ts");
                string routingContent = File.ReadAllText(routingPath);
                routingContent = routingContent.Replace("// CodeGeneration_Bookmark_Import代码生成书签勿删除", $"//{dto.DomainName}{ System.Environment.NewLine}import {{ {dto.DomainName}Component }} from './{dto.DomainName.ToLower()}/{dto.DomainName.ToLower()}.component';{ System.Environment.NewLine}// CodeGeneration_Bookmark_Import代码生成书签勿删除");
                string routingStr =$"{{ path: '{dto.DomainName.ToLower()}', component: {dto.DomainName}Component, data: {{ title: '{dto.DomainName}' }} }},{ System.Environment.NewLine}      // CodeGeneration_Bookmark_Routes代码生成书签勿删除无修改";
                routingContent = routingContent.Replace("// CodeGeneration_Bookmark_Routes代码生成书签勿删除无修改", routingStr);
                File.WriteAllText(routingPath, routingContent, Encoding.UTF8);
            }

        }
    }
}
